
package com.cti.commons.mqtt.domain;

import java.io.Serializable;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;

/**
 * @author et.wang
 * @description: 消息体
 * @date 2022/11/17 18:07
 */
@Data
public class MessageDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 消息ID
     */
    private String id;

    /**
     * 业务id
     */
    private String businessId;

    /**
     * 信息内容
     */
    private String payload;

    /**
     * 消息的时间戳
     */
    private Long timestamp;

    public static MessageDTO build(String payload) {
        if (StrUtil.isEmpty(payload)) {
            throw new RuntimeException("缺少构建消息必要参数");
        }
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setId(IdUtil.getSnowflakeNextIdStr());
        messageDTO.setPayload(payload);
        messageDTO.setTimestamp(System.currentTimeMillis());
        return messageDTO;
    }

    public static MessageDTO build(String businessId, String payload) {
        if (StrUtil.isEmpty(payload) || StrUtil.isEmpty(businessId)) {
            throw new RuntimeException("缺少构建消息必要参数");
        }
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setId(IdUtil.getSnowflakeNextIdStr());
        messageDTO.setBusinessId(businessId);
        messageDTO.setPayload(payload);
        messageDTO.setTimestamp(System.currentTimeMillis());
        return messageDTO;
    }

}
